/*
 * lossxml.h
 *
 *  Created on: 2008-12-2
 *      Author: root
 */

#ifndef LOSSXML_H_
#define LOSSXML_H_

#include "cxml.h"

//#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#define ID int

class LOSS_XML: public CXML
{
public:
	LOSS_XML();
	virtual ~LOSS_XML()
	{
	}
	;

	virtual void readDetail(const char *);
	virtual void formXML(char *);
	virtual int query(sqlite3 *);
	virtual int store(sqlite3 *);

	void setDelay(int);
	void setLoss(int);
	void setSrcId(ID);
	void setDesId(ID);
	void setSrcAddr(char*, char*);
	void setDesAddr(char*, char*);

	ID getSrcId();
	ID getDesId();
	int getLoss();
	int getDelay();
	void getSrcAddr();
	void getDesAddr();
private:
	int delay;			// ms
	int loss;				//%
	ID srcId;
	ID desId;
	char srcAddrType[16];								//inet 4 or inet6 etc.
	char desAddrType[16];
	char srcAddr[128];									//specific address
	char desAddr[128];
};

LOSS_XML::LOSS_XML()
{
	delay = 0;
	loss = 0;
}

void LOSS_XML::setLoss(int lossv)
{
	loss = lossv;
}

void LOSS_XML::setDelay(int delayv)
{
	delay = delayv;
}

void LOSS_XML::setSrcId(ID srcIdv)
{
	srcId = srcIdv;
}

void LOSS_XML::setDesId(ID desIdv)
{
	desId = desIdv;
}

void LOSS_XML::setSrcAddr(char *srcType, char *srcAddrv)
{
	strcpy(srcAddrType, srcType);
	strcpy(srcAddr, srcAddrv);
}

void LOSS_XML::setDesAddr(char *desType, char *desAddrv)
{
	strcpy(desAddrType, desType);
	strcpy(desAddr, desAddrv);
}


int LOSS_XML::getLoss()
{
	return loss;
}

int LOSS_XML::getDelay()
{
	return delay;
}

ID LOSS_XML::getSrcId()
{
	return srcId;
}

ID LOSS_XML::getDesId()
{
	return desId;
}

void LOSS_XML::readDetail(const char *)
{

}

void LOSS_XML::formXML(char *buf)
{
	char strTmp[128];
	//	time_t curTime;
	//	time(&curTime);
	TiXmlDocument xmlDoc;//("cpu_memory.xml"); // 建立一个XML文件
	TiXmlDeclaration Declaration("1.0", "UTF-8", "yes"); // 声明XML的属性
	xmlDoc.InsertEndChild(Declaration); // 写入基本的XML头结构
	TiXmlNode * pNode = NULL;
	TiXmlElement* pRootElm = NULL;
	TiXmlElement* pChildElm1 = NULL;
	TiXmlElement* pChildElm2 = NULL;
	TiXmlText* pText = NULL; // 一个指向Text的指针

	pRootElm = new TiXmlElement(title);
	pRootElm->SetAttribute("msgType", type);
	pNode = xmlDoc.InsertEndChild(*pRootElm); // first level (root), xmlDoc insert it

	pRootElm = pNode->ToElement();

	pChildElm1 = new TiXmlElement("operation");
	if (reqId)
		pChildElm1->SetAttribute("requestorId", reqId);
	pText = new TiXmlText(oper);
	pChildElm1->InsertEndChild(*pText); // child level 1
	pRootElm->InsertEndChild(*pChildElm1);
	delete pChildElm1;
	delete pText;

	pChildElm1 = new TiXmlElement("nodeId");
	sprintf(strTmp, "%d", nid);
	pText = new TiXmlText(strTmp);
	pChildElm1->InsertEndChild(*pText);
	pRootElm->InsertEndChild(*pChildElm1);
	delete pText;
	delete pChildElm1;

	pChildElm1 = new TiXmlElement("details");

	pNode = pRootElm->InsertEndChild(*pChildElm1);
	pRootElm = pNode->ToElement();

	pChildElm2 = new TiXmlElement("time"); // child level 2
	sprintf(strTmp, "%d", time);
	pText = new TiXmlText(strTmp);
	pChildElm2->InsertEndChild(*pText);
	pRootElm->InsertEndChild(*pChildElm2);
	delete pText;
	delete pChildElm2;

	pChildElm2 = new TiXmlElement("srcAddr");
	pChildElm2->SetAttribute("addrType", srcAddrType);
	pText = new TiXmlText(srcAddr);
	pChildElm2->InsertEndChild(*pText);
	pRootElm->InsertEndChild(*pChildElm2);
	delete pText;
	delete pChildElm2;

	pChildElm2 = new TiXmlElement("desAddr");
	pChildElm2->SetAttribute("addrType", desAddrType);
	pText = new TiXmlText(desAddr);
	pChildElm2->InsertEndChild(*pText);
	pRootElm->InsertEndChild(*pChildElm2);
	delete pText;
	delete pChildElm2;


	pChildElm2 = new TiXmlElement("loss");
	sprintf(strTmp, "%d", loss);
	pText = new TiXmlText(strTmp);
	pChildElm2->InsertEndChild(*pText);
	pRootElm->InsertEndChild(*pChildElm2);
	delete pText;
	delete pChildElm2;

	pChildElm2 = new TiXmlElement("delay");
	sprintf(strTmp, "%d", delay);
	pText = new TiXmlText(strTmp);
	pChildElm2->InsertEndChild(*pText);
	pRootElm->InsertEndChild(*pChildElm2);
	delete pText;
	delete pChildElm2;

	TiXmlPrinter printer;
	xmlDoc.Accept(&printer);
	strcpy(buf, printer.CStr());
//	return ();
}

int LOSS_XML::query(sqlite3 *)
{
	return 0;
}

int LOSS_XML::store(sqlite3 *)
{
	return 0;
}




#endif /* LOSSXML_H_ */
